<?php

namespace app\api\controller\ykjp\summary;

use app\common\controller\Api;
use think\Db;
use app\admin\model\ykjp\product\Product;
use app\admin\model\ykjp\information\basisinfo\Supplier;
use app\admin\model\ykjp\information\basisinfo\Warehouse;
use app\admin\model\ykjp\information\basisinfo\Partition;
use app\admin\model\ykjp\information\basisinfo\Customerinfo;

/**
 * 采购汇总
 *
 * @icon fa fa-circle-o
 */
class Purchase extends Api {

    protected $model = null;
    protected $noNeedRight = '*';

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * 查看
     *
     */
    public function index() {
		$this ->purchaseModel = new \app\admin\model\ykjp\purchase\Stoproduct;
		$total = $this ->purchaseModel ->where("status", "3")->count();
		$list = $this ->purchaseModel ->where("status", "3")->select();
				
		$prodcutModel = new Product;
		$supplierModel = new Supplier;
		$WarehouseModel = new Warehouse;
		$PartitionModel = new Partition;
		$totalNums = 0;
		$totalMoney = 0;
		foreach ($list as $row) {
			// var_dump($row);exit;
			$row['productName'] = "";
			$row['supplier'] = "";
			$row['warehouse'] = "";
			$row['partition'] = "";
			$productName = $prodcutModel->where("id", $row['product_id'])->Field("name")->find();
			if ($productName) {
				$row['productName'] = $productName["name"];
			}
				// $this->success('保存成功');

			$supplierName = $supplierModel->where("id", $row['storage']['supplier_id'])->Field("name")->find();
			if ($supplierName) {
				$row['supplier'] = $supplierName["name"];
			}

			$wareName = $WarehouseModel->where("id", $row['warehouse_id'])->Field("name")->find();
			if ($wareName) {
				$row['warehouse'] = $wareName["name"];
			}
			$PartName = $PartitionModel->where("id", $row['partition_id'])->Field("name")->find();
			if ($PartName) {
				$row['partition'] = $PartName["name"];
			}
			$totalNums += $row['nums'];
			$totalMoney += $row['subtotal'];
			$row->getRelation('storage')->visible(['id', "code", "inbound_type"]);
		}
		$list = collection($list)->toArray();
		$result = array("purchase" => array("totalNums" => $totalNums, "totalMoney" => $totalMoney));



        $this->Retprodcutmodel = new \app\admin\model\ykjp\purchase\Retprodcut;
		$total = $this ->Retprodcutmodel ->where("status", "3")->count();
		$list = $this ->Retprodcutmodel ->where("status", "3")->select();
		
		$totalNums = 0;
		$totalMoney = 0;
		foreach ($list as $row) {
			$row['productName'] = "";
			$row['supplier'] = "";
			$row['warehouse'] = "";
			$row['partition'] = "";
			$productName = $prodcutModel->where("id", $row['product_id'])->Field("name")->find();
			if ($productName) {
				$row['productName'] = $productName["name"];
			}

			$supplierName = $supplierModel->where("id", $row['retire']['supplier_id'])->Field("name")->find();
			if ($supplierName) {
				$row['supplier'] = $supplierName["name"];
			}

			$wareName = $WarehouseModel->where("id", $row['warehouse_id'])->Field("name")->find();
			if ($wareName) {
				$row['warehouse'] = $wareName["name"];
			}
			$totalNums += $row['nums'];
			$totalMoney += $row['subtotal'];
			$row->getRelation('retire')->visible(['id', "code"]);
		}
		
		$list = collection($list)->toArray();
		$result['Retprodcut'] = array("totalNums" => $totalNums, "totalMoney" => $totalMoney);
		
		
		
		$total = Db::name('ykjp_delivery') ->where("status_list", "6")->count();
		$list = Db::name('ykjp_delivery') ->where("status_list", "6")->select();
		$custModel = new Customerinfo;
		$nums = 0;
		$money = 0;
		foreach ($list as $row) {
		    $nums +=  Db::name('ykjp_delivery_product') ->where("id", $row['id'])->find()['nums'];
		    $money += Db::name('ykjp_delivery_product') ->where("id", $row['id'])->find()['subtotal'];
		}
		$result['deliveryPro'] = array("totalNums" => $nums, "totalMoney" => $money);
		
		
		
		
		$total = Db::name('ykjp_sell_return') ->where("status_list", "5")->count();
		$list = Db::name('ykjp_sell_return') ->where("status_list", "5")->select();
		$nums = 0;
		$money = 0;
		foreach ($list as $row) {
		    $nums +=  Db::name('ykjp_sell_return_p') ->where("id", $row['id'])->find()['nums'];
		    $money += Db::name('ykjp_sell_return_p') ->where("id", $row['id'])->find()['subtotal'];
		}
		$result['sellreturn'] = array("totalNums" => $nums, "totalMoney" => $money);

            // $this->success('保存成功');
		$this->success('返回成功',['data' => $result]);
    }

}
